﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
using ExaminationSys.DataAccess.Entity;

namespace ExaminationSys.View.Paper
{
    public partial class PaperFeedbackEdit : System.Web.UI.UserControl
    {
        private static readonly PaperFeedbackService Proxy = ObjectFactory<PaperFeedbackService>.Create();
        private static PaperFeedback entity = new PaperFeedback();
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!X.IsAjaxRequest)
            {
                entity = null;
                Bind();
            }
            
        }
        private void Bind()
        {

            //SectionType.DataSource = CommonEnum.EnumToDictionary(typeof(EnumSectionType));
            //this.SectionTypeID.GetStore().DataBind();
        }

        public Store GridStore { get; set; }
        public string ID { get; set; }

        public void SetData(string id, string paperFeedBackCommentID)
        {
        
            PaperFeedbackEditPanel.Reset();
            
            if (!string.IsNullOrEmpty(id))
            {
                var realId = new Guid(id);
                var PaperFeedback = Proxy.Get(realId);

                if (PaperFeedback != null)
                {
                    entity = PaperFeedback;
          
                    StartValue.Value = entity.StartValue;
                    EndValue.Value = entity.EndValue;
                    Comment.Text = entity.Comment;
                    OrderIndex.Value = entity.OrderIndex;
                    if (entity.Type == (int)EnumCommentType.按分数)
                    {
                        RadioPoint.Checked = true;
                    }
                    else
                    {
                        RadioTime.Checked = true;
                    }

                    if (PaperFeedback.ID != null)
                    {
                        id = PaperFeedback.ID.ToString();
                    }
                }
            }
            else
            {
                entity = new PaperFeedback();
            }
            X.Js.Call("setID", new object[] { id, paperFeedBackCommentID });
  
        }

        public void Show()
        {
            this.PaperFeedbackEditWindow.Show();
        }



        protected void SelectPaper_Click(object sender, DirectEventArgs e)
        {
           // CreateControl.CreateWindow("winCharacter", "角色选择", "../CommonWindow/CharacterSelection.aspx").Render(this.Form);
            //PaperListWindow.Visible = true;
            //PaperListWindow.Show();
        }

        protected void SavePaperFeedback(object sender, DirectEventArgs e)
        {
            if (!RadioPoint.Checked && !RadioTime.Checked)
            {
                MessageBoxHelper.ShowInfo("请选择一个评分标准!");
                return;
            }


            if (StartValue.Value==null || StartValue.Value.ToString() == string.Empty)
            {
                MessageBoxHelper.ShowInfo("评分开始值不能为空!");
                return; 
            }
            if (EndValue.Value ==null ||EndValue.Value.ToString() == string.Empty)
            {
                MessageBoxHelper.ShowInfo("评分结束值不能为空!");
                return;
            }

            if (OrderIndex.Value == null || OrderIndex.Value.ToString() == string.Empty)
            {
                MessageBoxHelper.ShowInfo("排序不能为空!");
                return;
            }

            entity.PaperFeedBackCommentID = new Guid(HidPaperFeedBackCommentID.Value);
            entity.StartValue = Convert.ToInt32(StartValue.Value);
            entity.EndValue = Convert.ToInt32(EndValue.Value);

            if (entity.StartValue > entity.EndValue)
            {
                MessageBoxHelper.ShowInfo("取值范围:开始值不能大于结束值!");
                return;
            }
            entity.OrderIndex = Convert.ToInt32(OrderIndex.Value);

         
           if (RadioPoint.Checked )
           {
               entity.Type = (int) EnumCommentType.按分数;
           }
           else
           {
               entity.Type = (int) EnumCommentType.按时间;
           }


            entity.Comment = Comment.Text;
             
            DateTime OpeatorTime = DateTime.Now;
            int status = 1;


            entity.Status = status;

            var op = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(op))
            {
                FormsAuthenticationHelper.SignOut();
                Response.Redirect("~/Account/Login.aspx");
            }
            entity.Operator = op;

            entity.OperatorTime = DateTime.Now;
            var sucess = false;
        
            if (string.IsNullOrWhiteSpace(IDHidden.Value))
            {
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                //MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                PaperFeedbackEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }

    }
}